package game.controller
{
	import flash.events.Event;
	
	import game.common.NetConnect;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.managers.ConfigManager;
	import game.managers.UIManager;
	import game.model.MenuModel;
	import game.view.menu.MenuPanel;
	import game.view.menu.SellPanel;

	public class MenuCenter extends BaseCenter
	{
		private static var _instance:MenuCenter;

		private var _menuPanel:MenuPanel;
		private var _sell:SellPanel;

		private var model:MenuModel;

		public function MenuCenter()
		{
			if (!_instance)
			{
				super();
				_instance=this;
				init();
				addEvent();
			}
			else
			{
				throw new Error("MainUICenter is a single class");
			}
		}

		private function addEvent():void
		{
			/**侦听打开菜单面板*/
			dispatcher.addEventListener(EventName.OPEN_MENU, openMenu);
			/**侦听打开购买材料面板*/
			dispatcher.addEventListener(EventName.OPEN_SELLPANEL, openSellPanel);
			/**侦听购买材料成功*/
			dispatcher.addEventListener(EventName.BUY_SUCCESS, buySuccess);
			/**侦听食物数量改变*/
			dispatcher.addEventListener(EventName.FOOD_CHANGE, foodChange);
			/**侦听金钱改变*/
			dispatcher.addEventListener(EventName.MONEY_CHANGE, moneyChange);
		}

		private var obj:Object;

		private function foodChange(e:GameEvent):void
		{
			obj=randomFood();

			_menuPanel.setFoodNum(obj.source);
			UIManager.orderMenu.setFoodNum(obj.source);

		}

		private function moneyChange(e:GameEvent):void
		{
			UIManager.mainUI.nowEx=UIManager.mainUI.nowEx + int(obj.ex);
			UIManager.mainUI.money=UIManager.mainUI.money + int(obj.price);
			Global.money=Global.money + int(obj.price);
			
			if (ConfigManager.CONNECT_SERVER) 
			{
				NetConnect.getInstance().call(obj);
			}
		}

		/**循环到数组里有属性的为止*/
		private function randomFood():Object
		{
			obj=Global.foodDataArr[Math.abs(Math.round(Math.random() * Global.foodDataArr.length - 1))]
			if (obj == null)
			{
				randomFood();
			}
			return obj;
		}


		private function checkSourceRepeat(item:*, index:int, arr:Array):Boolean
		{
			if (item == null)
			{
				arr[index]=obj;
				return true;
			}
			else if (item.source == obj.source)
			{
				return false;
			}
			else
				arr.push(obj);
			return true;

		}


		private function buySuccess(e:GameEvent):void
		{
			//Global.foodArr.push(e.param.source);
			if (Global.foodFull)
			{
				/**当食物格子满了之后,将除了3个选定的食材之外,其余食材的购买按钮设置为enabled=false*/
				_menuPanel.checkBuyBtnEnabled();
			}
			_menuPanel.setFoodType(e.param.source, e.param.num);
		}


		private function openMenu(e:GameEvent):void
		{
			if (!_menuPanel)
			{
				_menuPanel=new MenuPanel();
			}
			//_menuPanel.checkBuyBtnEnabled();
			_menuPanel.isPop=true;
		}

		private function openSellPanel(e:GameEvent):void
		{
			if (!_sell)
			{
				_sell=new SellPanel();
			}
			_sell.foodName=e.param.name;
			_sell.price=e.param.price;
			_sell.ex=e.param.ex;
			_sell.maxNum=Global.money / e.param.cost > 1500 ? 1500 : Global.money / e.param.cost;
			_sell.source=e.param.source;
			_sell.cost=e.param.cost;

			_sell.isPop=true;
		}

		public static function getInstance():MenuCenter
		{
			if (!_instance)
				_instance=new MenuCenter();
			return _instance;
		}


		private function init():void
		{
			_menuPanel=new MenuPanel();
			UIManager.menuPanel=_menuPanel;
			//LayerManager.windowLayer.addChild(_menuPanel);
			model=MenuModel.getInstance();

			for (var i:int=0; i < Global.foodDataArr.length; i++)
			{
				if (Global.foodDataArr[i] == null) 
				{
					continue;
				}
				_menuPanel.setFoodType(Global.foodDataArr[i].source, Global.foodDataArr[i].num,i);
			}


		}
	}
}